package com.sduept.nwld.dataserver.controller.daily;

import com.sduept.bigdata.web.jsf.controller.AbstractController;
import com.sduept.component.message.model.AlarmReceiveRole;
import com.sduept.core.ParamManager;
import com.sduept.core.entity.SystemParameter;
import com.sduept.nwld.dataserver.manager.protection.AlarmReceiveRoleManager;
import com.sduept.permission.entity.RoleEntityBean;
import com.sduept.permission.manager.RoleManager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import java.util.ArrayList;
import java.util.List;

@Named
@ViewScoped
public class AlarmReceiveRoleController extends AbstractController {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8621744867618000249L;
	@Autowired
	private AlarmReceiveRoleManager arm;
	@Autowired
	private RoleManager roleM;
	@Autowired
	private ParamManager pcache;
	
	private List<AlarmReceiveRole> alarmReceiveRoles = new ArrayList<AlarmReceiveRole>();
	private AlarmReceiveRole alarmReceiveRole = new AlarmReceiveRole();
	
	private SystemParameter selectMsgType = new SystemParameter();
	private List<SystemParameter> msgTypes = new ArrayList<SystemParameter>();
	private SystemParameter msgType = new SystemParameter();
	
	private List<RoleEntityBean> allRoles = new ArrayList<RoleEntityBean>();
	private List<String> selectRoles = new ArrayList<String>();
	@PostConstruct
	public void init() {
		alarmReceiveRoles = arm.findAllAlarmReceiveRole();
		allRoles = roleM.findAllRoles();
		// 获取所有的消息类型
		msgTypes = arm.initAlarmParams();
	}

	/**
	 * 获取当前告警类型对应的AlarmReceiveRole
	 * 如果allReceive == true,父部门也需要接收短信
	 * 如果allReceive == false,父部门不接收短信（仅有该变电站的直属父部门下的人员能够收到短信）
	 * @param param
	 * @return
	 */
	public String findReceiveType(SystemParameter param) {
		Boolean flag = arm.ifAllReceive(param.getId());
		String result = "";
		if(null != flag) {
			if(true == flag) {
				result = "是";
			} else if(false == flag) {
				result = "否";
			}
		}
		return result;
	}
	
	public void preEdit() {
		selectRoles.clear();
		List<AlarmReceiveRole> list = arm.findByAlarmType(selectMsgType.getId());
		if(list.size() > 0) {
			alarmReceiveRole = list.get(0);
			for (AlarmReceiveRole alarmRole : list) {
				if(StringUtils.isNotEmpty(alarmRole.getRoleId())) {
					selectRoles.add(alarmRole.getRoleId());
				}
			}
		} else if (list.size() == 0) {
			alarmReceiveRole = new AlarmReceiveRole();
			alarmReceiveRole.setName(selectMsgType.getName());
			alarmReceiveRole.setAlarmType(selectMsgType.getId());
		}
	}
	public void save() {
		// 需要将当前消息类型的所有记录全部删除，再新增新的角色
		arm.deleteByAlarmType(alarmReceiveRole.getAlarmType());
		if(selectRoles.size()>0) {
			for (RoleEntityBean role : allRoles) {
				if(selectRoles.contains(role.getId())) {
					AlarmReceiveRole record = new AlarmReceiveRole();
					record.setAlarmType(alarmReceiveRole.getAlarmType());
					record.setAllReceive(alarmReceiveRole.isAllReceive());
					record.setName(alarmReceiveRole.getName());
					record.setReceiveRole(role.getName());
					record.setRoleId(role.getId());
					arm.createOrUpdate(record);
				}
			}
		}
		selectMsgType = null;
		addSuccessMessage("保存成功！");
	}
	/**
	 * 移除某一告警类型的所有短信接收角色
	 */
	public void delete() {
		arm.deleteByAlarmType(selectMsgType.getId());
		addSuccessMessage("移除角色成功！");
		selectMsgType = null;
	}

	/**
	 * 获得某一告警类型的所有短信接收角色
	 * @param param
	 * @return
	 */
	public List<String> getAllReceiveRole(SystemParameter param) {
		List<String> roles = new ArrayList<String>();
		List<AlarmReceiveRole> records = arm.findByAlarmType(param.getId());
		for (AlarmReceiveRole alarmReceiveRole : records) {
			if(null != alarmReceiveRole) {
				String role = alarmReceiveRole.getReceiveRole();
				if(StringUtils.isNotEmpty(role)) {
					roles.add(role);
				}
			}
		}
		return roles;
	}
	/**
	 * 移除某个告警类型的单个角色
	 * @param role
	 * @param param
	 */
	public void removeReceiveRole(String roleName, SystemParameter param) {
		List<AlarmReceiveRole> list = arm.queryByCondition(param.getId(), roleName, null);
		for (AlarmReceiveRole alarmReceiveRole : list) {
			arm.delete(alarmReceiveRole.getId());
		}
		addSuccessMessage("移除成功！");
	}
	
	public List<AlarmReceiveRole> getAlarmReceiveRoles() {
		return alarmReceiveRoles;
	}

	public void setAlarmReceiveRoles(List<AlarmReceiveRole> alarmReceiveRoles) {
		this.alarmReceiveRoles = alarmReceiveRoles;
	}

	public AlarmReceiveRole getAlarmReceiveRole() {
		return alarmReceiveRole;
	}

	public void setAlarmReceiveRole(AlarmReceiveRole alarmReceiveRole) {
		this.alarmReceiveRole = alarmReceiveRole;
	}

	public List<SystemParameter> getMsgTypes() {
		return msgTypes;
	}

	public void setMsgTypes(List<SystemParameter> msgTypes) {
		this.msgTypes = msgTypes;
	}

	public List<RoleEntityBean> getAllRoles() {
		return allRoles;
	}

	public void setAllRoles(List<RoleEntityBean> allRoles) {
		this.allRoles = allRoles;
	}

	public SystemParameter getMsgType() {
		return msgType;
	}

	public void setMsgType(SystemParameter msgType) {
		this.msgType = msgType;
	}

	public List<String> getSelectRoles() {
		return selectRoles;
	}

	public void setSelectRoles(List<String> selectRoles) {
		this.selectRoles = selectRoles;
	}

	public SystemParameter getSelectMsgType() {
		return selectMsgType;
	}

	public void setSelectMsgType(SystemParameter selectMsgType) {
		this.selectMsgType = selectMsgType;
	}

}
